package reflect;

import inc.CommonFunction;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;

import lottery.Lottery;
import dao.Hdao;

public class ReflectObject implements ReflectService
{

    public static void main(String[] args) throws ClassNotFoundException
    {    
        Lottery lt = new Lottery();
        HashMap map = getFieldValues(lt);
        Iterator iterator = map.keySet().iterator();
        while(iterator.hasNext()) {
            System.out.println(iterator.next());
        }
    }
    
    
    public static String getXMLFieldValues(String className, String condition){
        CommonFunction fun = new CommonFunction();
        HashMap map = null;
        Iterator keyIterator = null;
        String key = null;
        String results = fun.xmlAppend(null, null, "resp", null);
        ArrayList list = Hdao.query(className, condition);
        if(list.size()>0){
            results = fun.xmlAppend(results, "resp", "list", null);
        }
        results = fun.xmlAppend(results, "resp", "size", list.size());
        for(Object obj : list){
            results = fun.xmlAppend(results, "list", "obj", null);
            map = ReflectObject.getFieldValues(obj, className);
            keyIterator = map.keySet().iterator();
            while(keyIterator.hasNext()){
                key = (String) keyIterator.next();
                results = fun.xmlAppend(results, "obj", key, map.get(key));          
            }
        }
        System.out.println("getXMLFieldValues():" + results);
        return results;
    }

    /**
     * 执行obj的get方法并返回属性值
     * excute the method get, return  values of fields
     * @param obj
     * @return
     * @author liuding,2012-3-30
     */
    public static HashMap getFieldValues(Object obj)
    {
        HashMap value = new HashMap();
        String prefix = "get"; // 目标方法前缀
        Class clazz = obj.getClass();
        Method[] ms = clazz.getMethods();
        String methodName = "";
        String fieldName = ""; // 属性名
        Object fieldValue = null;//属性值
        for (Method m : ms)
        {
            methodName = m.getName();
            // 如果是get方法且是自定义方法
            if (methodName.startsWith(prefix) && !ReflectHelper.isDefineMethod(m.getName()))
            {
                fieldName = methodName.substring(prefix.length(), methodName.length());
                fieldName = (String) (fieldName.charAt(0) + "").toLowerCase()
                        + fieldName.substring(1, fieldName.length());   
                try
                {
                    fieldValue = m.invoke(obj, null);
                    value.put(fieldName, fieldValue);
                }
                catch (Exception e)
                {
                    // TODO Auto-generated catch block
                    System.out.println("reflect.ReflectObject.getProperties" + e);
                    e.printStackTrace();
                }
            }
        }
        return value;
    }
    
    /**
     * 执行obj的get方法并返回属性值
     * excute the method get, return  values of fields
     * @param obj
     * @return
     * @author liuding,2012-3-30
     */
    public static HashMap getFieldValues(Object obj, String className)
    {
        HashMap value = new HashMap();
        String prefix = "get"; // 目标方法前缀
        Class clazz = null;
        try
        {
            clazz = Class.forName(className);
        }
        catch (ClassNotFoundException e1)
        {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        Method[] ms = clazz.getMethods();
        String methodName = "";
        String fieldName = ""; // 属性名
        Object fieldValue = null;//属性值
        for (Method m : ms)
        {
            methodName = m.getName();
            // 如果是get方法且是自定义方法
            if (methodName.startsWith(prefix) && ReflectHelper.isDefineMethod(m.getName()))
            {
                fieldName = methodName.substring(prefix.length(), methodName.length());
                fieldName = (String) (fieldName.charAt(0) + "").toLowerCase()
                        + fieldName.substring(1, fieldName.length());   
                try
                {
                	if(m.getParameterTypes().length <=0 ){
	                    fieldValue = m.invoke(obj, null);
	                    value.put(fieldName, fieldValue);
                	}
                }
                catch (Exception e)
                {
                    // TODO Auto-generated catch block
                    System.out.println("reflect.ReflectObject.getProperties" + e);
                    e.printStackTrace();
                }
            }
        }
        return value;
    }
    
    
}
